"""Conversation models for the trading platform."""

from sqlalchemy import Column, Integer, String, Text, DateTime, Boolean, ForeignKey, Index, func, BigInteger
from sqlalchemy.orm import relationship
from database import Base


class Message(Base):
    """Message model representing individual messages in conversations."""

    __tablename__ = 'messages'

    id = Column(Integer, primary_key=True, autoincrement=True)
    sender_id = Column(Integer, ForeignKey('users.id'), nullable=False)
    receiver_id = Column(Integer, ForeignKey('users.id'), nullable=False)
    conversation_id = Column(Integer, ForeignKey('conversations.id'), nullable=False)
    content = Column(Text, nullable=False)
    is_read = Column(Boolean, default=False)
    reply_to_id = Column(Integer, ForeignKey('messages.id'))
    created_at = Column(DateTime, default=func.now())
    read_at = Column(DateTime)

    # Relationships
    sender = relationship("User", foreign_keys=[sender_id])
    receiver = relationship("User", foreign_keys=[receiver_id])
    replies = relationship("Message", back_populates="parent_message", foreign_keys=[reply_to_id])
    parent_message = relationship("Message", remote_side=[id], back_populates="replies", foreign_keys=[reply_to_id])
    conversation = relationship("Conversation", back_populates="messages", foreign_keys=[conversation_id])

    # Indexes
    __table_args__ = (
        Index('idx_sender_id', 'sender_id'),
        Index('idx_receiver_id', 'receiver_id'),
        Index('idx_conv_participant_conversation_id', 'conversation_id'),
        Index('idx_msg_created_at', 'created_at'),
        Index('idx_msg_is_read', 'is_read'),
    )


class Conversation(Base):
    """Conversation model representing chat conversations between users."""

    __tablename__ = 'conversations'

    id = Column(Integer, primary_key=True, autoincrement=True)
    user1_id = Column(Integer, ForeignKey('users.id'), nullable=False)
    user2_id = Column(Integer, ForeignKey('users.id'), nullable=False)
    last_message_id = Column(Integer, ForeignKey('messages.id'))
    last_message_time = Column(DateTime)
    user1_unread_count = Column(Integer, default=0)
    user2_unread_count = Column(Integer, default=0)
    user1_archived = Column(Boolean, default=False)
    user2_archived = Column(Boolean, default=False)
    created_at = Column(DateTime, default=func.now())
    updated_at = Column(DateTime, default=func.now(), onupdate=func.now())

    # Relationships
    user1 = relationship("User", foreign_keys=[user1_id])
    user2 = relationship("User", foreign_keys=[user2_id])
    last_message = relationship("Message", foreign_keys=[last_message_id])
    participants = relationship("ConversationParticipant", back_populates="conversation")
    messages = relationship("Message", back_populates="conversation", foreign_keys="Message.conversation_id")

    # Indexes
    __table_args__ = (
        Index('idx_user1_id', 'user1_id'),
        Index('idx_user2_id', 'user2_id'),
        Index('idx_last_message_time', 'last_message_time'),
    )


class ConversationParticipant(Base):
    """ConversationParticipant model representing user participation in conversations."""

    __tablename__ = 'conversation_participants'

    id = Column(Integer, primary_key=True, autoincrement=True)
    conversation_id = Column(Integer, ForeignKey('conversations.id'), nullable=False)
    user_id = Column(Integer, ForeignKey('users.id'), nullable=False)
    unread_count = Column(Integer)
    is_archived = Column(Boolean)
    joined_at = Column(DateTime, default=func.now())

    # Relationships
    conversation = relationship("Conversation", back_populates="participants")
    user = relationship("User")

    # Indexes
    __table_args__ = (
        Index('idx_conversation_id', 'conversation_id'),
        Index('idx_conv_participant_user_id', 'user_id'),
    )